package com.qf.hospital.controller;

import com.alibaba.druid.util.StringUtils;
import com.qf.hospital.enums.HospitalErrorEnums;
import com.qf.hospital.vo.R;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Administrator
 * @version V1.0
 * @Project hospital
 * @Package com.qf.hospital.controller
 * @Description: ${TODO}
 * @Date 2021/10/15 15:19
 */

@RestController
public class LoginController {

    private Logger log = LoggerFactory.getLogger(LoginController.class);

    @PostMapping("/sel/doLogin")
    public R login(String loginname, String pwd){
        //1. 校验请求参数
        if (StringUtils.isEmpty(loginname) || StringUtils.isEmpty(pwd)){
            log.info("【登录功能】 请求参数异常，用户名和密码均为必填项！");
            return R.error(HospitalErrorEnums.USERNAME_PASSWORD_ERROR,"用户名和密码不能为空");
        }

        //2. 获取Subject主体
        Subject subject = SecurityUtils.getSubject();
        //3. 执行认证
        subject.login(new UsernamePasswordToken(loginname, pwd));
        //4. 登录成功
        return R.ok("登录成功");
    }

    @GetMapping("/sel/logout")
    public R logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return R.ok("退出登录");
    }
}
